package com.putable.nmc.world;


import junit.framework.TestCase;
/**
 * Some basic tests for the XRandom class
 * @author ackley
 *
 */
public class TestXRandom extends TestCase {
    private static final int GENERATORS = 10;
    private XRandom[] gens = new XRandom[GENERATORS];

    public void setUp() {
        long seed = 1;
        for (int g = 0; g < GENERATORS; ++g) {
            gens[g] = new XRandom(seed);
            seed = 27*seed+113;
        }
    }
    
    public void testExponential() {
        for (int g = 0; g < GENERATORS; ++g) {
            for (double mean = 1; mean <= 1000; mean *= 10) {
                double sum = 0.0;
                final int testCount = 1000;
                for (int tests = 0; tests < testCount; ++tests)
                    sum += gens[g].nextExponential(mean);

                double average = sum/testCount;
                double window = .80;
                assertTrue("Computed average too big", average >= mean*window);
                assertTrue("Computed average too small", average <= mean/window);
            }
        }   
    }

    public void testNextProbability() {
        for (double p = -1.0; p < 2.0; p += 0.3) {
            boolean threw = false;
            try { gens[0].nextProbability(p); }
            catch (IllegalArgumentException e) { threw = true; }
            assertEquals(threw, p<0.0 || p>1.0);
        }

        for (int g = 0; g < GENERATORS; ++g) {
            for (double prob= 0.0; prob<= 1.0; prob += 0.10) {
                double sum = 0.0;
                final int testCount = 5000;
                for (int tests = 0; tests < testCount; ++tests)
                    if (gens[g].nextProbability(prob)) ++sum;

                double average = sum/testCount;
                double window = .80;
                assertTrue("Hit too often (actual: "
                           +average+", expected: "+prob,
                           average >= prob*window);
                assertTrue("Hit too often (rarely: "
                            +average+", expected: "+prob,
                            average <= prob/window);
            }
        }   
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestXRandom.class);
    }

}
